package com.yoomiito.app.ui.anyview.goodsinfo.header

import android.content.Context
import android.graphics.Color
import androidx.constraintlayout.widget.ConstraintLayout
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.yoomiito.app.*
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.ui.goods.NewGoodsInfoActivity
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.item_goods_detail_price_1.view.*
import kotlinx.android.synthetic.main.item_upgrade_vip.view.*
import kotlinx.android.synthetic.main.video_play_view.view.*
import kotlinx.android.synthetic.main.view_goodsinfo_header.view.*

class YouPinGoodsInfoHeaderView(context: BaseActivity<*>) : BaseGoodsInfoHeaderView(context) {
    override fun getLayoutId(): Int = R.layout.view_goodsinfo_header

    override fun getParentView(): LinearLayout = this

    override fun initView(goodsDetail: GoodsDetail) {
        initBanner(banner, goodsDetail)
        getVideoTime(goodsDetail) {
            playVideoView.visibility = View.VISIBLE
            durationTv.text = DateUtils.ms2min(it.duration)
            playVideoView.setOnClickListener { _ -> OnVideoClickListener(it) }
        }

        //价格背景
        if (viewStubIsInflate(R.id.priceLlVs)){
            priceLlVs.layoutResource = R.layout.item_goods_detail_price_1
            priceLlVs.inflate()
        }
        bg_1.setImageResource(R.drawable.bg_youpin_1)
        bg_2.setImageResource(R.drawable.bg_youpin_2)
        couponView.visibility = View.GONE
        //价格
        price.setTextColor(ContextCompat.getColor(context,R.color.colorAccent))
        val priceT = goodsDetail.salePrice.removeDecimalPoint()
//        if (!App.mAppConfig.isLogin || null == goodsDetail.coin || goodsDetail.coin.isEmptyOr0() || goodsDetail.yo_money_not == 0) {
//            val priceStr = if (goodsDetail.reserve_price.isEmptyOr0()) "¥${priceT} " else "¥${priceT} ¥${goodsDetail.reserve_price.removeDecimalPoint()}"
//            price.text = priceStr.goodsPrice(priceT, true, R.color.color_FEBBBA, 0.375f)
//        } else {
//            if (goodsDetail.reserve_price.isEmptyOr0()) {
//                oldPrice.visibility = View.GONE
//            } else {
//                val oldP = goodsDetail.reserve_price.removeDecimalPoint()
//                val oldPriceSpan = SpannableString("¥${oldP}")
//                oldPriceSpan.setSpan(StrikethroughSpan(), 0, oldP.length + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)//原价划线
//                oldPrice.text = oldPriceSpan
//                oldPrice.visibility = View.VISIBLE
//            }
//            val prce11 = goodsDetail.cash.removeDecimalPoint()
//            val priceStr = "¥$prce11 +${goodsDetail.yo_money_not}优米"
//            val spannableString = SpannableString(priceStr)
//            spannableString.setSpan(RelativeSizeSpan(0.53f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            spannableString.setSpan(RelativeSizeSpan(0.65f), (if (prce11.contains(".")) prce11.indexOf(".") else prce11.length) + 1, priceStr.length - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            spannableString.setSpan(RelativeSizeSpan(0.45f), prce11.length + 2, priceStr.length - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            spannableString.setSpan(RelativeSizeSpan(0.28f), priceStr.length - 2, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            spannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_FFEAB4)), prce11.length + 2, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            price.text = spannableString
//        }
        if (!App.mAppConfig.isLogin || null == goodsDetail.user_deduction_account || goodsDetail.user_deduction_account.isEmptyOr0() || goodsDetail.deduction_account.isEmptyOr0()) {
            val priceStr = if (goodsDetail.reserve_price.isEmptyOr0()) "¥${priceT} " else "¥${priceT} ¥${goodsDetail.reserve_price.removeDecimalPoint()}"
            price.text = priceStr.goodsPrice(priceT, true, R.color.colorAccent, 0.375f)
        } else {
            if (goodsDetail.reserve_price.isEmptyOr0()) {
                oldPrice.visibility = View.GONE
            } else {
                val oldP = goodsDetail.reserve_price.removeDecimalPoint()
                val oldPriceSpan = SpannableString("¥${oldP}")
                oldPriceSpan.setSpan(StrikethroughSpan(), 0, oldP.length + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)//原价划线
                oldPrice.text = oldPriceSpan
                oldPrice.visibility = View.VISIBLE
            }
            val priceStr = "¥$priceT 可抵扣¥${goodsDetail.deduction_account}"
//            val priceStr = "¥$priceT 可抵扣¥18.92"
            val spannableString = SpannableString(priceStr)
            spannableString.setSpan(RelativeSizeSpan(0.53f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spannableString.setSpan(RelativeSizeSpan(0.53f),  if (priceT.contains(".")) priceT.indexOf(".") + 1 else priceStr.indexOf(" "), priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            if (priceT.contains(".")){
//                spannableString.setSpan(RelativeSizeSpan(0.65f),  priceT.indexOf(".") + 1, priceStr.length - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            }
//            spannableString.setSpan(RelativeSizeSpan(0.45f), priceT.length + 2, priceStr.length - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//            spannableString.setSpan(RelativeSizeSpan(0.28f), priceStr.length - 2, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.colorAccent)), priceT.length + 2, priceStr.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            price.text = spannableString
        }
        //升级VIP
        if (UserInfoConfig.getRoleId() < Type.LevelType.VIP_7) {
            if (viewStubIsInflate(R.id.upgradeVipVs)){
                val view = upgradeVipVs.inflate()
                val lp1 = view.layoutParams as LinearLayout.LayoutParams
                lp1.topMargin = DensityUtil.dp2px(12f)
            }
            VIPAwardTv.text = "¥${goodsDetail.upgrade_money}"
            upgradeVip.setOnClickListener {
                RouterUtils.toMain(context as NewGoodsInfoActivity, 1)
            }
        }

        //标题
        goodsTitleTv.text = goodsDetail.title
        goodsDesTv.text = goodsDetail.recommended_language
        goodsDesTv.visibility = if (goodsDetail.recommended_language.isNullOrEmpty()) View.GONE else View.VISIBLE
        goodsTitleCopy(goodsTitleTv,goodsDetail.title)

        //销量 及分享件数
        val size = "已抢${goodsDetail.volume}件"
        val sizeSpannableString = SpannableString(size)
        sizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, size.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//销量颜色
        goodsSaleSizeTv.text = sizeSpannableString

        val shareSize = "分享${goodsDetail.share_num}次"
        val shareSizeSpannableString = SpannableString(shareSize)
        shareSizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, shareSize.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//分享件数颜色
        goodsShareSizeTv.text = shareSizeSpannableString

        if (viewStubIsInflate(R.id.itemGoodsVs)){
            itemGoodsVs.layoutResource = R.layout.item_goods_flag_1
            itemGoodsVs.inflate()
        }
    }

}